< previous page page_573 next page >

Page 573
Type.gifListing 14.26. Coding the LogError method.
Private Sub LogError(strErrMsg As String)
    '
    ' write error to disk file
    On Error GoTo LocalErr
    '
    Dim intChFile As Integer
    '
    intChFile = FreeFile
    Open errLogFileName For Append As intChFile
        Print #intChFile, Format(Now, "general date")
        Print #intChFile, strErrMsg
        Print #intChFile, ""
    Close intChFile
    '
    Exit Sub
    '
LocalErr:
    ' trouble with file stuff!
    Err.Raise vbObjectError + 1, "errHandler.LogError", "Can't write log file ["
& errLogFileName & "]"
    '
End Sub
Notice that you added an error handler in this routine. Because you are about to perform disk operations, you need to be ready for errors here, too! Notice also that the internal error is not displayed in a message box. Instead the Raise method of the Err object is used to generate a unique error number and description. This is sent back to the calling application for handling.
Tip.gif
The Visual Basic FreeFile() function is used to return a number that represents the first available file channel that Visual Basic uses to open the data file. Using FreeFile() guarantees that you do not select a file channel that Visual Basic is already using for another file.

Now all you need to do is add a call to the LogError method from within the Public errHandler method. Listing 14.27 shows the code you need to add in the routine. Make sure you add these lines of code right after the call to the errMsg function and just before the start of the Select Case statement.

 
< previous page page_573 next page >

If you like this book, buy it!